package com.eastmoney.emis.edda.domain.valueobject;

import com.eastmoney.emis.edda.domain.entity.ServiceTime;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.function.Predicate;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain.valueobject
 * @author: 汪程程
 * @date: 2022/10/15 9:56 AM
 * @version: 1.0
 */
public enum ServiceTimeType implements Predicate<ServiceTime> {
    WEEK_DAY(1) {
        @Override
        public boolean test(ServiceTime serviceTime) {
            LocalDateTime now = LocalDateTime.now();
            int weekDay = now.getDayOfWeek().getValue();
            if (weekDay > 5) {
                return false;
            }
            return EVERY_DAY.test(serviceTime);
        }
    },
    EVERY_DAY(2) {
        @Override
        public boolean test(ServiceTime serviceTime) {
            LocalTime nowTime = LocalTime.now();
            return inDuration(nowTime, serviceTime.getAmOpen(), serviceTime.getAmClose()) ||
                    inDuration(nowTime, serviceTime.getPmOpen(), serviceTime.getPmClose());
        }
    },
    HK_TRADE_DAY(3) {
        @Override
        public boolean test(ServiceTime serviceTime) {
            // 需要获取交易日 一起判断
            return EVERY_DAY.test(serviceTime);
        }
    };

    private final Integer value;

    ServiceTimeType(Integer value) {
        this.value = value;
    }

    private static final ServiceTimeType[] ENUMS = ServiceTimeType.values();

    public static ServiceTimeType parse(Integer value) {
        for (ServiceTimeType serviceTimeType : ENUMS) {
            if (serviceTimeType.value.equals(value)) {
                return serviceTimeType;
            }
        }
        return null;
    }

    private static boolean inDuration(LocalTime target, LocalTime start, LocalTime end) {
        return target.equals(start) || target.equals(end) || (target.isAfter(start) && target.isBefore(end));
    }

}
